package cn.tedu.collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SortListDemo2 {
    public static void main(String[] args) {
        List<Point> list = new ArrayList<>();
        list.add(new Point(1,2));
        list.add(new Point(11,32));
        list.add(new Point(23,46));
        list.add(new Point(67,48));
        list.add(new Point(78,35));
        list.add(new Point(72,56));
        list.add(new Point(24,58));
        System.out.println("排序之前的:"+list);
        Collections.sort(list, (o1, o2) -> o1.getX() * o1.getX() + o1.getY() * o1.getY() -
                o2.getX() * o2.getX() - o2.getY() * o2.getY());
        System.out.println(list);
//        MyComparator c = new MyComparator();
//        Collections.sort(list,c);
//        System.out.println(list);

//        Comparator<Point> com = new Comparator<Point>() {
//            @Override
//            public int compare(Point o1, Point o2) {
//                int len1 = o1.getX()*o1.getX() + o1.getY()*o1.getY();
//                int len2 = o2.getX()*o2.getX() + o2.getY()*o2.getY();
//                return len1-len2;
//            }
//        };
//        Collections.sort(list,com);

    }
}
class MyComparator implements Comparator<Point>{

    @Override
    public int compare(Point o1, Point o2) {
        int len1 = o1.getX()*o1.getX()+o1.getY()*o1.getY();
        int len2 = o2.getX()*o2.getX()+o2.getY()*o2.getY();
        return len1-len2;
    }
}